package gqz.skin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import gqz.skin.R;

/**
 * Created by Administrator on 2017/11/27.
 */

public class ListViewAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;

    public final int type_listview = 0;
    public final int type_gridview = 1;
    int type = type_listview;

    ArrayList<String> textlist = new ArrayList();
    ArrayList<Integer> iconlist = new ArrayList();

    public ListViewAdapter(Context context, ArrayList<String> textlist, ArrayList<Integer> iconlist) {
        this.context = context;
        this.textlist = textlist;
        this.iconlist = iconlist;
        inflater = LayoutInflater.from(context);
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return textlist.size();
    }

    @Override
    public Object getItem(int position) {
        return textlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case type_listview:
                    convertView = inflater.inflate(R.layout.item_listview, null);
                    holder.iv = (ImageView) convertView.findViewById(R.id.item_list_iv);
                    holder.tv = (TextView) convertView.findViewById(R.id.item_list_tv1);
                    break;
                case type_gridview:
                    convertView = inflater.inflate(R.layout.item_gridview, null);
                    holder.iv = (ImageView) convertView.findViewById(R.id.item_grid_iv);
                    holder.tv = (TextView) convertView.findViewById(R.id.item_grid_tv1);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //
        holder.iv.setBackground(context.getResources().getDrawable(iconlist.get(position)));
        holder.tv.setText(textlist.get(position));

        return convertView;
    }

    class ViewHolder {
        ImageView iv;
        TextView tv;
    }
}
